package com.cmcc.es.app.service.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;

import com.cmcc.es.app.mapper.ExpertSyncMapper;
import com.cmcc.es.app.model.ExpertInfo;
import com.cmcc.es.app.model.ExpertSyncData;
import com.cmcc.es.app.service.DataSyncService;

import cc.movein.common.utils.XmlTools;
/**
 * 
 * 专家用户信息同步
 * @author WZX
 *
 */
@Service("expertSyncService")

public class ExpertSyncServiceImpl implements DataSyncService {
	
	private Log log = LogFactory.getLog(ExpertSyncServiceImpl.class);
	private static final String DATA_FORMAT = "yyyy-MM-dd HH:mm:ss";
	@Autowired
	private ExpertSyncMapper expertSyncMapper;
	
	@Transactional
	@Override
	public void doSync(String xml) {
		String decodedXml = null;
		try {
			decodedXml = new String(Base64Utils.decodeFromString(xml), "utf-8");
			log.debug(decodedXml);
			ExpertSyncData data = XmlTools.xmlStringToBean(decodedXml, ExpertSyncData.class, DATA_FORMAT);
			log.info(data);
			List<ExpertInfo> updateList = data.getUpdateList();
			List<ExpertInfo> insertList = data.getInsertList();
			if(updateList != null && updateList.size()>0){
				for (ExpertInfo expertInfo : updateList) {
					expertSyncMapper.updateItem(expertInfo);
				}
			}
			if(insertList != null && insertList.size()>0){
				expertSyncMapper.insertList(insertList);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void do(String xml){
		String decodedXml = null;
		try {
			decodedXml = new String(Base64Utils.decodeFromString(xml), "utf-8");
			log.debug(decodedXml);
			ExpertSyncData data = XmlTools.xmlStringToBean(decodedXml, ExpertSyncData.class, DATA_FORMAT);
			log.info(data);
			List<ExpertInfo> updateList = data.getUpdateList();
			List<ExpertInfo> insertList = data.getInsertList();
			if(updateList != null && updateList.size()>0){
				for (ExpertInfo expertInfo : updateList) {
					expertSyncMapper.updateItem(expertInfo);
                    hello world
				}
			}
			if(insertList != null && insertList.size()>0){
				expertSyncMapper.insertList(insertList);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}